//
//  CellOfContent.m
//  PetAPP
//
//  Created by dllo on 16/1/26.
//  Copyright © 2016年 Yuming. All rights reserved.
//

#import "CellOfContent.h"


@interface CellOfContent ()


@end

@implementation CellOfContent

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubViewsWithFrame:(CGRect){.size = frame.size}];
    }
    return self;
}

- (void)createSubViewsWithFrame:(CGRect)frame {
#pragma mark - 详细信息
    self.imageViewOfAuthorFace = [[UIImageView alloc] init];
    [self addSubview:self.imageViewOfAuthorFace];
    [_imageViewOfAuthorFace release];
    self.imageViewOfAuthorFace.layer.cornerRadius = 25;
    self.imageViewOfAuthorFace.layer.masksToBounds = YES;
    /** 更改初始宽度 */
    self.labelOfAuthor = [[UILabel alloc] init];
    [self addSubview:self.labelOfAuthor];
    [_labelOfAuthor release];
    
    
    
    self.labelOfDateline = [[UILabel alloc] init];
    [self addSubview:self.labelOfDateline];
    [_labelOfDateline release];
    self.labelOfDateline.textColor = [UIColor grayColor];
    
    self.labelOfAddress = [[UILabel alloc] init];
    [self addSubview:self.labelOfAddress];
    [_labelOfAddress release];
    self.labelOfAddress.textColor = [UIColor grayColor];
    
#pragma mark ** 添加关注
    self.ButtonOfLike = [UIButton buttonWithType:UIButtonTypeCustom];
    self.ButtonOfLike.frame = CGRectMake(frame.size.width - (15 + 80), 30, 80, 30);
    [self addSubview:self.ButtonOfLike];
    self.ButtonOfLike.titleLabel.font = [UIFont systemFontOfSize:17];
    [self.ButtonOfLike setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
    self.ButtonOfLike.layer.cornerRadius = 4;

    
    self.labelOfMessage = [[UILabel alloc] init];
    [self addSubview:self.labelOfMessage];
    [_labelOfMessage release];
    self.labelOfMessage.numberOfLines = 0;
    
    
    self.viewOftag = [[UIView alloc] init];
    [self addSubview:self.viewOftag];
    [_viewOftag release];
    
    UIImageView *imageViewOfMark = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
    imageViewOfMark.image = [UIImage imageNamed:@"biaoqian"];
    for (int i = 0 ; i < 5; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.viewOftag addSubview:button];
        button.titleLabel.font = [UIFont systemFontOfSize:17];
        
        [button setTitleColor:[UIColor colorWithRed:0.0796 green:0.2411 blue:0.9941 alpha:1.0]forState:UIControlStateNormal];
        
        button.tag = i + 100;
    }
    [self.viewOftag addSubview:imageViewOfMark];
    [imageViewOfMark release];
    
    
    self.viewOfpics = [[UIView alloc] init];
    
    [self addSubview:self.viewOfpics];
    [_viewOfpics release];
    self.viewOfpics.backgroundColor = [UIColor whiteColor];
    /** 设置图片个数 */
    for (int i = 0; i < 9; i++) {
        UIImageView *view = [[UIImageView alloc] init];
        [self.viewOfpics addSubview:view];
        [view release];
        view.tag = i + 10;
    }
    
    self.recomView = [[UIView alloc] init];
    [self addSubview:self.recomView];
    [_recomView release];
    
    self.labelOfRecommendtotal = [[UILabel alloc] init];
    
    [self.recomView addSubview:self.labelOfRecommendtotal];
    [_labelOfRecommendtotal release];
    self.labelOfRecommendtotal.textColor = [UIColor grayColor];;
    
    self.imageViewOfLike = [[UIImageView alloc] init];
    [self.recomView addSubview:self.imageViewOfLike];
    [_imageViewOfLike release];
    
    
    self.labelOfReplies = [[UILabel alloc] init];
    [self.recomView addSubview:self.labelOfReplies];
    [_labelOfReplies release];
    self.labelOfReplies.textColor = [UIColor grayColor];
    
    
    self.imageViewOfReplies = [[UIImageView alloc] init];
    
    [self.recomView addSubview:self.imageViewOfReplies];
    [_imageViewOfReplies release];
    
    
   
}

- (void)passContentWithModel:(ModelOfContent *)model {
    
#pragma mark - 添加关注
    if (model.isfollow == 0) {/**< 已经关注 */
        self.ButtonOfLike.backgroundColor = [UIColor whiteColor];
        self.ButtonOfLike.layer.borderWidth = 0;
        [self.ButtonOfLike setTitle:@"已关注" forState:UIControlStateNormal];
    }else {
        self.ButtonOfLike.backgroundColor = [UIColor colorWithRed:1.0 green:0.8423 blue:0.2839 alpha:1.0];
        self.ButtonOfLike.layer.borderWidth = 0;
        [self.ButtonOfLike setTitle:@"✚ 关注" forState:UIControlStateNormal];
    }
    
    /** 设置初始宽度 */
    CGFloat beginWidth = 10;
    /** 设置初始高度 */
    self.beginHeight = 20;

#pragma mark ** 设置用户照片高度
    self.imageViewOfAuthorFace.yy_imageURL = [NSURL URLWithString:model.userface];
    
    self.imageViewOfAuthorFace.frame = CGRectMake(beginWidth, self.beginHeight, 50, 50);
#pragma mark ** 设置用户名字高度
    self.labelOfAuthor.text = model.author;
    
    beginWidth += self.imageViewOfAuthorFace.frame.size.width + 10;
    self.labelOfAuthor.frame = CGRectMake(beginWidth, self.beginHeight, 200, 25);
#pragma mark ** 设置最后回复时间高度
    
    self.beginHeight += self.labelOfAuthor.frame.size.height + 5;
    
    NSDate *tempDate = [NSDate dateWithTimeIntervalSince1970:model.dateline.intValue];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    [dateFormatter setDateFormat:@"HH:mm"];
    NSString *strDate = [dateFormatter stringFromDate:tempDate];
    [dateFormatter setDateFormat:@"MM-dd"];
    NSString *str2Date = [dateFormatter stringFromDate:tempDate];
    [dateFormatter release];
    
    NSDate *now = [NSDate date];
    NSTimeInterval time = [now timeIntervalSince1970];
    float seconds = (int)time - model.dateline.doubleValue;
    float points = seconds / 60;
    if (points < 60) {
        self.labelOfDateline.text = [NSString stringWithFormat:@"%d分钟前", (int)points];
        
    }else if (points > 60 && points < 3600) {
        
        self.labelOfDateline.text = [NSString stringWithFormat:@"%@",strDate];
    }else {
        self.labelOfDateline.text = [NSString stringWithFormat:@"%@",str2Date];
    }

    CGRect dateText = [self.labelOfDateline.text boundingRectWithSize:CGSizeMake(0, self.labelOfDateline.frame.size.height) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:17]} context:nil];
    self.labelOfDateline.frame = CGRectMake(beginWidth, self.beginHeight, dateText.size.width, 20);
    
#pragma mark ** 设置用户地址高度
    self.labelOfAddress.text = model.address;
    
    beginWidth += self.labelOfDateline.frame.size.width + 10;
    self.labelOfAddress.frame = CGRectMake(beginWidth, self.beginHeight, self.labelOfAuthor.frame.size.width - self.labelOfDateline.frame.size.width - 10, 20);

#pragma mark ** 设置简介的自适应高度
    
    self.labelOfMessage.text = model.message;
    /** 内容图层高度 */
    
    self.beginHeight += self.labelOfDateline.frame.size.height + 15;
    self.labelOfMessage.frame = CGRectMake(10, self.beginHeight, self.frame.size.width - 10 * 2, 20);
    /** 设置自适应高度 */
    CGRect frame = [self.labelOfMessage.text boundingRectWithSize:CGSizeMake(self.labelOfMessage.frame.size.width, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:17]} context:nil];
    CGRect messageFrame = self.labelOfMessage.frame;
    self.labelOfMessage.frame = CGRectMake(messageFrame.origin.x, messageFrame.origin.y, messageFrame.size.width, frame.size.height);
    
    

#pragma mark ** 设置标签的图层高度
    beginWidth = 10;
    /** 标签图层高度 */
    self.beginHeight += self.labelOfMessage.frame.size.height + 15;
    self.viewOftag.frame = CGRectMake(beginWidth, self.beginHeight, self.frame.size.width - beginWidth * 2, 20);
    
    NSArray *tagArr = model.tags;
    for (int i = 0; i < tagArr.count; i++) {
        NSString *str = [tagArr[i] objectForKey:@"tagname"];
        
        CGRect buttonFrame = [str boundingRectWithSize:CGSizeMake(0, 18) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:17]} context:nil];
        
        UIButton *button = [self viewWithTag:(i + 100)];
        UIButton *lastButton = [self viewWithTag:(i + 99)];
        [button setTitle:str forState:UIControlStateNormal];
        if (i != 0) {
            button.frame = CGRectMake(lastButton.frame.origin.x + lastButton.frame.size.width + i * 5, 0, buttonFrame.size.width + 10, 20);
        }else {
            button.frame = CGRectMake(20, 0, buttonFrame.size.width + 10, 20);
        }
        
    }
    
    
    
    
#pragma mark ** 设置图片图层高度
    /** 设置图片图层高度 */
    self.beginHeight += self.viewOftag.frame.size.height + 10;
    
//    picNum.backgroundColor = [UIColor blackColor];
//    picNum.alpha = 0.2;
    
    
    
    [self setPicViewOfHeightWithModel:model];
    UILabel *picNum = nil;
    if (model.pics.count < 9) {
        picNum = [[UILabel alloc] initWithFrame:CGRectMake(self.viewOfpics.frame.size.width - 20, self.viewOfpics.frame.size.height - 20, 20, 20)];
    }else {
        picNum = [[UILabel alloc] initWithFrame:CGRectMake(self.viewOfpics.frame.size.width - 20, self.viewOfpics.frame.size.height - 17, 20, 20)];
    }
    
    [self.viewOfpics addSubview:picNum];
    [picNum release];
    picNum.font = [UIFont systemFontOfSize:17];
    picNum.textColor = [UIColor whiteColor];
    picNum.text = model.picnum;
    picNum.textAlignment = NSTextAlignmentCenter;
    
    
    UIBezierPath *backgroundPath = [UIBezierPath bezierPathWithRect:CGRectMake(0, 3, 20, 20)];
    
    picNum.layer.shadowPath = backgroundPath.CGPath;
    picNum.layer.shadowOpacity = 0.4;
    picNum.layer.shadowRadius = 0;
    picNum.layer.shadowColor = [UIColor blackColor].CGColor;
    
    
    
    
    self.beginHeight += self.viewOfpics.frame.size.height + 10;
    [self setRecomViewHeightWithModel:model];
    
    self.beginHeight += 5;
    
}



#pragma mark -设置图层高度
- (void)setPicViewOfHeightWithModel:(ModelOfContent *)model {


}
#pragma mark - 设置推荐图层高度
- (void)setRecomViewHeightWithModel:(ModelOfContent *)model {
#pragma mark ** 设置推荐的高度
    
    self.labelOfRecommendtotal.text = model.recommendtotal;
    /** 创建推荐高度*/
    
    self.recomView.frame = CGRectMake(0 , self.beginHeight, self.frame.size.width, 15);
    
    
    CGFloat beginWidth = self.frame.size.width - 10 - 15;
    self.labelOfRecommendtotal.frame = CGRectMake(beginWidth , 0, 30, 15);
#pragma mark ** 设置推荐图片的高度
    /** 推荐图片 */
    self.imageViewOfLike.image = [UIImage imageNamed:@"kongxin"];
    /** 推荐图标大小 */
    beginWidth -= self.labelOfRecommendtotal.frame.size.width - 10;
    self.imageViewOfLike.frame = CGRectMake(beginWidth, 0, 15, 15);
#pragma mark ** 设置讨论人数
    self.labelOfReplies.text = model.replies;
    /** 设置评论宽度 */
    beginWidth -= self.imageViewOfLike.frame.size.width + 20;
    self.labelOfReplies.frame = CGRectMake(beginWidth, 0, 30, 15);
#pragma mark ** 评论图片宽度
    /** 回复图片 */
    self.imageViewOfReplies.image = [UIImage imageNamed:@"huifu"];
    /** 设置评论图片宽度 */
    beginWidth -= self.labelOfReplies.frame.size.width - 10;
    self.imageViewOfReplies.frame = CGRectMake(beginWidth, 0, 15, 15);


}




#pragma mark - 设置整体大小
+ (CGFloat)returnCGSizeForModel:(ModelOfContent *)model {
    CGFloat picHeight = 0;
    if (model.pics.count == 2) {
        picHeight = 200;
        
    } else if (model.pics.count == 3) {
       picHeight = 150;
        
    } else if (model.pics.count >= 6 && model.pics.count <= 8) {
        picHeight = 300;
        
    } else {
       picHeight = 400;
        
    }
    CGRect frame = [model.message boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 20, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:17]} context:nil];
    CGFloat num = 20 + 50 + 15 + frame.size.height + 15 + 20 + 10 + picHeight + 10 + 15 + 5 ;
    return num;
}



/** collectionCell 的子控件的布局通常在此方法中设置 */
- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {
    
}




- (void)dealloc {
    [_imageViewOfAuthorFace release];
    [_labelOfAuthor release];
    [_labelOfDateline release];
    [_labelOfAddress release];
    [_ButtonOfLike release];
    [_labelOfMessage release];
    [_viewOftag release];
    [_tagOfArr release];
    [_imageViewOfReplies release];
    [_labelOfReplies release];
    [_labelOfRecommendtotal release];
    [_imageViewOfLike release];
    [_viewOfpics release];
    [super dealloc];
}

@end
